﻿using Iworld.basis.Service;
using Iworld.Game.Manager;
using Iworld.Game.Model;
using Iworld.Game.Service.Client.Export;
using Iworld.Game.Service.Client.Import;
using Iworld.Main.Impl;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Iworld.Game.Service.Client
{
    /// <summary>
    /// 前台游戏服务
    /// </summary>
    public class GameService : IGameService
    {

        /// <summary>
        /// 获取当前游戏信息
        /// </summary>
        /// <returns></returns>
        public NormalResult<GameChapterExport> GetCurrentGameInfo(GetCurrentGameInfoImport import)
        {
            try
            {
          
                using (ModelToDbContext db = new ModelToDbContext())
                {
                    import.CheckAllowExecuteOrNot(db);
                    string tk = GameManager.GetCurrentTk();
                    GameChapter game = db.GameChapters.Where(x => x.Tk== tk 
                        &&  x.Status == Iworld.Game.Model.GameChapterStatus.正在开奖).FirstOrDefault();
                    GameChapter lastGame = null;
                    //没有正在开奖的牌轮
                    if(game == null)
                    {
                        game = db.GameChapters.Where(x => x.Tk == tk && x.ActiveTime != null &&
                            x.Status == Iworld.Game.Model.GameChapterStatus.从未开奖).FirstOrDefault();
                        if (game != null && game.ChapterNumber > 1)
                        {  
                            int last = game.ChapterNumber -1;
                            lastGame = db.GameChapters.Where(x => x.Tk == tk && x.ChapterNumber == last &&
                            x.Status == Iworld.Game.Model.GameChapterStatus.开奖结束).FirstOrDefault();
                        }
                    }
                    return new NormalResult<GameChapterExport>(new GameChapterExport(game,lastGame));
                }
            }
            catch (Exception ex)
            {
                return new NormalResult<GameChapterExport>(null,ex.Message);
            }
        }

        /// <summary>
        /// 获取自身投注记录
        /// </summary>
        /// <param name="import"></param>
        /// <returns></returns>
        public PageResult<BettingRecordExport> GetBettingRecords(GetBettingRecordsImport import)
        {
            try
            {
                using(ModelToDbContext db = new ModelToDbContext())
                {
                    import.CheckAllowExecuteOrNot(db);
                    return import.GetBettingRecords(db);
                }
            }
            catch (Exception ex)
            {
                return new PageResult<BettingRecordExport>(ex.Message);
            }
        }

        /// <summary>
        /// 投注
        /// </summary>
        /// <param name="import"></param>
        /// <returns></returns>
        public NormalResult<BettingResultExport> Betting(BettingImport import)
        {
            try
            {
                using (ModelToDbContext db = new ModelToDbContext())
                {
                    import.CheckAllowExecuteOrNot(db);
                    Iworld.Game.Manager.BettingManager.TempBetting tm = new BettingManager(db).Betting(import);
                    BettingResultExport be = new BettingResultExport(tm);
                    return new NormalResult<BettingResultExport>(be);
                }
            }
            catch (Exception ex)
            {
                return new NormalResult<BettingResultExport>(null, ex.Message);
            }
        }

        /// <summary>
        /// 获取当前投注信息
        /// </summary>
        /// <param name="import"></param>
        /// <returns></returns>
        public PageResult<CurrentBettingUsersInfoExport> GetCurrentBettingUsersInfo(GetCurrentBettingUsersInfoImport import)
        {
             int? pageSize = 10;
             int? start = (import.PageIndex - 1) * pageSize;
             List<Iworld.Game.Manager.BettingManager.TempBetting> currentBettingInfos = BettingManager.GetCurrentBettingsInfo(start, pageSize);
             List<CurrentBettingUsersInfoExport> rets = new List<CurrentBettingUsersInfoExport>();
             currentBettingInfos.ForEach(x => {
                 rets.Add(new CurrentBettingUsersInfoExport(x)); 
             });
             return new PageResult<CurrentBettingUsersInfoExport>(import.PageIndex, rets.Count, (int)pageSize, rets);
        }


    }
}
